package com.aitesting.common;

import lombok.Getter;

/*状态码组件封装*/
@Getter
public class CommonResult<T> {

    /**
     * 状态码
     */
    private final long code;

    /**
     * 消息提示
     */
    private final String message;

    /**
     * 响应体
     */
    private final T data;

    public CommonResult(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    //公共方法，data为null
    public static <T> CommonResult common(IErrorCode commonCode) {
        return new CommonResult<>(commonCode.getCode(), commonCode.getMessage(), null);
    }

    //公共方法，返回自定义data
    public static <T> CommonResult common(IErrorCode commonCode, T data) {
        return new CommonResult<>(commonCode.getCode(), commonCode.getMessage(), data);
    }

    //使用默认message
    public static <T> CommonResult success(T data) {
        return common(CommonCode.SUCCESS, data);
    }

    //自定义message
    public static <T> CommonResult success(T data, String message) {
        return new CommonResult<>(CommonCode.SUCCESS.getCode(), message, data);
    }

    //使用默认message
    public static <T> CommonResult failed() {
        return common(CommonCode.FAILURE);
    }

    //自定义message
    public static <T> CommonResult failed(String message) {
        return new CommonResult<>(CommonCode.FAILURE.getCode(), message, null);
    }

    //使用默认message
    public static <T> CommonResult forbid() {
        return common(CommonCode.FORBIDDEN);
    }

    //自定义message
    public static <T> CommonResult forbid(String message) {
        return new CommonResult<>(CommonCode.FORBIDDEN.getCode(), message, null);
    }
}
